﻿/// <reference path="../../../plugins/layui/plugins/eleTree/eleTree.js" />
/// <reference path="../../../plugins/layui/plugins/eleTree/eleTree.js" />
$(function () {
    //2.初始化页面
    var oPageInit = new PageInit();
    oPageInit.Init();
    //oPageInit.LoadRoleEmployee();
});

var PageInit = function () {
    var oInit = new Object();
    var postdata = {};

    var table, form, layer, tree, util;
    var active;
    var tableId = 'tb_list';

    //初始化页面上面的按钮事件
    oInit.Init = function () {
        layui.use(['table', 'tree', 'util'], function () {
            table = layui.table
          , tree = layui.tree
          , util = layui.util
          , form = layui.form
          , layer = layui.layer;
            oInit.loadData();
        })
    }

    //初始化Table,加载数据
    oInit.loadData = function () {
        //方法级渲染
        table.render({
            elem: '#' + tableId
            , id: tableId
        , loading: true
            //, skin: 'line' //line （行边框风格） row （列边框风格） nob （无边框风格）
        , even: false //开启隔行背景
        , size: 'lg' //小尺寸的表格
        , toolbar: '#toolbar'
            , where: { htid: document.getElementById('ht_id').value }
        , defaultToolbar: ['']  //工具栏右侧的图标按钮
        , url: Root + 'HTType1Title/GetHTType1Title'
        , cols: [[
        { checkbox: true, fixed: true }, { type: 'numbers', title: '序号' }
        , {
            field: 'ht_title',
            title: '荣誉标题',
            width: 200
        }, {
            field: 'ft_title',
            title: '附件标题'
        }, {
            field: 'ft_sort',
            title: '排序',
            width: 100
        }, {
            fixed: 'right', title: '操作', toolbar: '#tableBar', width: 120,
        }
        ]]
        , page: true
        , limit: 10
            //, cellMinWidth: 80
        , height: 'full-300'
        });

        var $ = layui.$;
        active = {
            reload: function () {
                var txt_search = $('#txt_search');
                var temp = {
                    SearchText: txt_search.val(),
                    htid: document.getElementById('ht_id').value
                };
                //执行重载
                table.reload(tableId, {
                    method: 'post',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                  , where: temp
                }, 'data');
            },
            back: function () {
                location.href = '/EPHonor/index';
            }
        };

        $('#formSearch .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //var newEventsCode = 'if (obj.event === "add") { $("#selHt_id").val($("#ht_id").val()); }';
        //监听工具条增删改按钮
        zpActive.tableTool(table, layer, form, null, null, null, null, oInit.NewEventsCode, oInit.Delete, null);


        //监听提交按钮
        form.on('submit(save)', function (data) {
            var rs = oInit.Save(data.field);    //保存数据
            if (rs) {
                active.reload();    //重新加载table数据
                layer.closeAll();   //关闭弹出层
            }
            return false;   //true刷新当前页面
        });

        //form.on('select(selHt_type)', function (data) {
        //    var val = data.value;
        //    var text = data.elem[data.elem.selectedIndex].text;
        //    if (val == 2) { //文字类型
        //        $('#type2_div').show();
        //    } else {
        //        $('#type2_div').hide();
        //        $('#selHt_pid').val('');
        //        form.render();
        //    }
        //});

        /*form.on('select(selHt_pid)', function (data) {
            //var val = data.value;
            var text = data.elem[data.elem.selectedIndex].text;
            $('#txtEmployeeName').val(text);
        });*/

        oInit.LoadEPHonorTitle();
    }

    oInit.NewEventsCode = function (formSelector, obj) {
        if (obj.event == 'add') {
            $(formSelector).find('[name="ft_id"]').val('0');
            $("#selHt_id").val($("#ht_id").val());
        }
    }

    //保存-新增
    oInit.Save = function (formdata) {
        //var dto = decodeURIComponent(formdata, true);   //处理中文,后台需要解码
        var result = false;
        //静态添加节点
        $.ajax({
            type: "post",
            url: Root + 'HTType1Title/SaveHTType1Title',
            async: false,
            data: { dto: formdata },
            success: function (data) {
                if (data.SubmitResult) {
                    result = true;
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
        return result;
    }

    //删除
    oInit.Delete = function (arr_obj) {
        $.ajax({
            type: "post",
            url: Root + 'HTType1Title/DeleteHTType1Title',
            data: { dto: arr_obj },
            success: function (data) {
                if (data.SubmitResult) {
                    toastr.success('删除成功');
                    active.reload();
                    layer.closeAll();   //关闭所有弹出层
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () { }
        });
    }

    //加载下拉荣誉标题
    oInit.LoadEPHonorTitle = function () {
        //加载角色信息
        var selectId = '#selHt_id';
        $.ajax({
            type: "post",
            async: false,
            //dataType:'json',
            url: Root + 'EPHonor/GetAllEPHonorTitle',
            data: { ht_type: 1},//1.附件，2.文字，3.数字
            success: function (data) {
                if (data.SubmitResult) {
                    var select = $(selectId);
                    select.empty();
                    select.append('<option value="">无</option>');
                    $.each(data.Object, function (i, item) {
                        select.append('<option value="' + item.ht_id + '">' + item.ht_title + '</option>');
                    });
                } else {
                    dialog.danger(data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () { }
        });
    }
    return oInit;
}